<?php

namespace App\Console\Commands;

use App\depository\TsBatchDep;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use App\depository\TsOrderDep;

class ImportCmd extends Command
{

    public $signature = 'order:import';

    public function handle()
    {

        $this->info('ok');

        $batchDep = new TsBatchDep();
        $orderDep = new TsOrderDep();

        $task = $batchDep->getWait();
        if ($task->isEmpty()) {
            $this->log('没有待执行任务');
            return false;
        }

        $config = ['path' => storage_path("/app/orders/")];
        $excel = new \Vtiful\Kernel\Excel($config);

        $task->each(function ($item) use ($excel, $batchDep, $orderDep) {

            $this->info("开始处理，{$item->batch_no}");
            $batchDep->upd($item->id, ['status' => 2]);

            $file = storage_path("/app/orders/{$item->batch_no}.xlsx");
            if (!file_exists($file)) {
                $this->log("没有找到文件,{$item->batch_no}");
                $batchDep->upd($item->id, ['status'=> 4]);
                return true;
            }

            $allData = $excel->openFile("{$item->batch_no}.xlsx")->openSheet()->getSheetData();
            $expRows = count($allData);
            if ($expRows <= 0) {
                $this->log("文件里没有数据,{$item->batch_no}");
                return true;
            }

            $updData = [
                'expect_rows' => $expRows
            ];
            $batchDep->upd($item->id, $updData);

            $chunkArr = array_chunk($allData, 500);

            $rows = 0;
            foreach ($chunkArr as $key => $arr) {
                $data = [];
                foreach ($arr as $row) {
                    $data[] = [
                        "batch_no" => $item->batch_no,
                        "name" => $row[0],
                        "orderid" => $row[1],
                        "getprovince" => $row[2],
                        "getcity" => $row[3],
                        "count" => $item->count,
                    ];
                }
                $count = count($data);
                $rows = $count + $rows;

                $this->info("处理数量,{$count}");
                $orderDep->add($data);

                $updData = [
                    'rows' => $rows,
                    'rate' => number_format($rows / $expRows, 2) * 100
                ];
                $batchDep->upd($item->id,$updData);
            }

            $updData = [
                "status" => 3
            ];
            $batchDep->upd($item->id, $updData);
            $this->info("处理完成,{$item->batch_no}");
        });

        $this->info("处理完成,{$this->getMemoryUsage()}");
    }

    public function log($message)
    {
        $this->info($message);
        Log::info($message);
    }

    function getMemoryUsage($precision = 2)
    {
        $size = memory_get_usage(true);

        $unit = ['b', 'kb', 'mb', 'gb', 'tb', 'pb'];

        return round($size / pow(1024, ($i = floor(log($size, 1024)))), $precision) . ' ' . $unit[$i];
    }

}
